﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Library;

namespace OA.Exam
{
    public partial class SetPaperQuestions : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string paperId = Request["paperId"].ToString();
                HiddenPaperId.Value = paperId;
                //绑定可添加的题目类型
                BindCanAddType();

                //绑定现有大题类型
                BindBigType();
                //预览试卷
                HylPreview.NavigateUrl = "PreviewPaper.aspx?paperId=" + paperId;
            }
        }

        protected void BindCanAddType()
        {
            DdlType.Items.Clear();
            DdlType.Items.Add(new ListItem("-请选择-", "-1"));
            string sql = "select TypeId,TypeName from Exam_QuestionTypes where TypeID not in (select ItemType from Exam_PapersQuestionTypes where paperId = '" + HiddenPaperId.Value + "')";
            DataTable dt = DataAccess.getDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                for(int i=0;i<dt.Rows.Count;i++)
                {
                    DdlType.Items.Add(new ListItem(dt.Rows[i]["TypeName"].ToString(), dt.Rows[i]["TypeId"].ToString()));
                }
            }
        }

        protected void BindBigType()
        {
            string sql = "select ItemType,TypeName,Score,DispOrder from Exam_PapersQuestionTypes left join Exam_QuestionTypes on ItemType=TypeId  where paperId = '" + HiddenPaperId.Value + "'";
            DataTable dt = DataAccess.getDataTable(sql);
            DataListBig.DataSource = dt.DefaultView;
            DataListBig.DataBind();
        }

        protected void DataListBig_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string paperId = HiddenPaperId.Value;
            string typeId = DataListBig.DataKeys[e.Item.ItemIndex].ToString();
            if (e.CommandName == "DeleteType")
            {
                if (myExam.DeletePaperBigType(paperId, typeId))
                {
                    LabPrompt.Text = "删除成功";
                    BindBigType();
                    BindCanAddType();
                }
            }
            if (e.CommandName == "AddQueston")
            {
                Response.Redirect("MaintainPaperQuestions.aspx?paperId=" + paperId + "&typeId=" + typeId);
            }
        }

        protected void LbtnAddType_Click(object sender, EventArgs e)
        {
            string paperId = HiddenPaperId.Value;
            string typeId = DdlType.SelectedValue;
            if (myExam.InsertPaperBigType(paperId, typeId))
            {
                LabPrompt.Text = "添加成功";
                BindBigType();
                BindCanAddType();
            }
        }

        protected void LbtnSave_Click(object sender, EventArgs e)
        {
            string paperId = HiddenPaperId.Value;
            for (int i = 0; i < DataListBig.Items.Count; i++)
            {
                string typeId = DataListBig.DataKeys[i].ToString();
                int score = int.Parse(((TextBox)DataListBig.Items[i].FindControl("TxtScore")).Text.ToString());
                int dispOrder = int.Parse(((TextBox)DataListBig.Items[i].FindControl("TxtDispOrder")).Text.ToString());
                if (myExam.UpdatePaperBigType(paperId, typeId,score,dispOrder))
                {
                    LabPrompt.Text = "保存成功";
                }
            }
        }
        //主观题不批量设置分值
        protected void DataListBig_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string typeId = DataListBig.DataKeys[e.Item.ItemIndex].ToString();
                if (typeId == "00004" || typeId == "00005")
                {
                    ((TextBox)e.Item.FindControl("TxtScore")).Text = "1";
                    ((TextBox)e.Item.FindControl("TxtScore")).ReadOnly = true;
                }
            }
        }

        protected void LbtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("PapersManage.aspx");
        }
    }
}
